<?php 
    $servername = "127.0.0.1";
    $username = "test_ynhbg_com";
    $password = "z2wR5fXj6yY5aZM9";
    $dbname = "test_ynhbg_com";
    
    // 创建数据库连接
    $con = new mysqli($servername, $username, $password, $dbname);
    if ($con->connect_error) {
        die("连接失败: " . $con->connect_error);
    }
    if (isset($_GET['newcode'])) 
    {
        $sql = "INSERT INTO ay_visit_log (status) VALUES ('".$_GET['newcode']."')";
        mysqli_query($con, $sql);
        echo "<script>window.location.href='qudao.php';</script>";
    }
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>推广码管理</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
        }
        th, td {
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        #addCodeForm {
            margin-top: 20px;
        }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/qrcode@1.4.4/build/qrcode.min.js"></script>
</head>
<body>
    <h1>推广码管理</h1>
    <table id="promoTable">
        <thead>
            <tr>
                <th>推广码</th>
                <th>推广链接</th>
                <th>推广人数</th>
                <th>累计访问</th>
            </tr>
        </thead>
        <tbody>
            <?php 
                $sql = "SELECT status FROM ay_visit_log GROUP BY status ORDER BY status DESC";
                $result = $con->query($sql);
                
                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                        $status = $row['status'];
                        
                        $sqlIP = "SELECT IP FROM ay_visit_log WHERE status='$status' GROUP BY IP";
                        $resultIP = $con->query($sqlIP);
                        $IPnum = $resultIP->num_rows-1;
                        
                        $sqlIP1 = "SELECT IP FROM ay_visit_log WHERE status='$status'";
                        $resultIP1 = $con->query($sqlIP1);
                        $IPnum1 = $resultIP1->num_rows-1;
                        
                        echo "
                        <tr>
                            <td><canvas id='qrcode_$status'></canvas></td>
                            <td>https://test.ynhbg.com/?qudao=$status</td>
                            <td>$IPnum</td>
                            <td>$IPnum1</td>
                        </tr>
                        <script>
                            QRCode.toCanvas(document.getElementById('qrcode_$status'), 'https://test.ynhbg.com/?qudao=$status', function (error) {
                                if (error) console.error(error);
                            });
                        </script>
                        ";
                    }
                }
                $con->close();
            ?>
        </tbody>
    </table>
    <?php
        $con = new mysqli($servername, $username, $password, $dbname);
        $sql1 = "SELECT status FROM ay_visit_log GROUP BY status ORDER BY status DESC";
        $find1 = mysqli_query($con, $sql1);
        $show1 = mysqli_fetch_array($find1);
    ?>
    <a href="https://test.ynhbg.com/apps/admin/view/default/log/qudao.php?newcode=<?php echo $show1['status']+1; ?>">新增推广码</a>

</body>
</html>
